#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int longestSubsequence(vector<int>& arr, int difference) {
    int n = arr.size(), ret = 1;
    unordered_map<int, int> hash;
    hash[arr[0]] = 1;

    for (int i = 1; i < n; i++) {
        hash[arr[i]] = hash[arr[i] - difference] + 1;
        ret = max(ret, hash[arr[i]]);
    }

    return ret;
}

int main()
{
    vector<int>arr = { 1, 5, 7, 8, 5, 3, 4, 2,};
    int difference = -2;
    cout << longestSubsequence(arr, difference) << endl;
    return 0;
}